package edu.metrostate.capstonediner;

import java.util.ArrayList;

public class PartBuffer extends SpriteBuffer {
	// Contains a list of all of the parts that need to be displayed
	private ArrayList<DrawablePart> partsList = new ArrayList<DrawablePart>();
	// Used to store the texture for sprites
	PartTexture partTexture;
	
	public void setTexture(PartTexture texture) {
		this.texture = texture;
		partTexture = texture;
	}
	
	public void add(DrawablePart part) {
		synchronized(listLock) {
			partsList.add(part);
			size = partsList.size();
			
			// Checks to see if buffers need to be increased in size
			if (partsList.size() > maxSprites) {
				createBuffers(partsList.size() * 2);
			}
		}
		
		update();
	}
	
	public void remove(DrawablePart part) {
		synchronized(listLock) {
			partsList.remove(part);
			size = partsList.size();
		}
		
		update();
	}
	
	public void update() {
		float x;
		float y;
		float width;
		float height;
		
		float textureX;
		float textureY;
		float textureWidth;
		float textureHeight;
		
		DrawablePart part;
		
		// Used to store the current index for the vertices
		int verticesIndex = 0;
		// Used to store the current index for the indices
		short indicesIndex = 0;
		
		synchronized(listLock) {
			if (texture == null) {
				return;
			}
			
			//get each part's location
			for (int i = 0; i < partsList.size(); i++) {
				part = partsList.get(i);
				x = part.getX();
				y = part.getY();
				width = part.getWidth();
				height = part.getHeight();
				
				textureX = partTexture.getX(part.getTexture());
				textureY = partTexture.getY(part.getTexture());
				textureWidth = partTexture.getWidth(part.getTexture());
				textureHeight = partTexture.getHeight(part.getTexture());
				
				verticesList[verticesIndex++] = x;
				verticesList[verticesIndex++] = y;
				verticesList[verticesIndex++] = textureX;
				verticesList[verticesIndex++] = textureY;
				verticesList[verticesIndex++] = x;
				verticesList[verticesIndex++] = y + height;
				verticesList[verticesIndex++] = textureX;
				verticesList[verticesIndex++] = textureY + textureHeight;
				verticesList[verticesIndex++] = x + width;
				verticesList[verticesIndex++] = y;
				verticesList[verticesIndex++] = textureX + textureWidth;
				verticesList[verticesIndex++] = textureY;
				verticesList[verticesIndex++] = x + width;
				verticesList[verticesIndex++] = y + height;
				verticesList[verticesIndex++] = textureX + textureWidth;
				verticesList[verticesIndex++] = textureY + textureHeight;
				
				indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * i + 0);
				indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * i + 1);
				indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * i + 2);
				indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * i + 1);
				indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * i + 2);
				indicesList[indicesIndex++] = (short) (VERTICES_PER_SPRITE * i + 3);
			}
		}
	}
}
